

// 公共的模块

const crypto = require("crypto");   // Node 自带API 

// MD5 只能加密不能解密 
// 加密函数  data 需要加密的字段 
function aesEncrypt(data, key) {
    data = data.toString();
    const cipher = crypto.createCipher('aes192', key);
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;  // 密文  
}

// 解密 
function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher('aes192', key);
    var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;  // 明文 
}

const keys = "wuhan2004daydayup";   //   abc123   abc123wuhan2004daydayup

exports.aesEncrypt = aesEncrypt;   // 加密
exports.aesDecrypt = aesDecrypt;   // 解密
exports.keys = keys;        // 密钥 

function startEncrypt(data){
    return aesEncrypt(data,keys);  // 加密
}

function getDecrypt(encrypted){
    return aesDecrypt(encrypted,keys);  // 解密 
}

// exports.startEncrypt = (data)=>aesEncrypt(data,keys);
exports.startEncrypt = startEncrypt
exports.getDecrypt = getDecrypt

//查看是否是登录状态
exports.checkSession = function(req,res,callback){
    if(req.session.username){
        callback(); // 回调函数
    }else{
        res.send("<script>alert('Session已经过期无效,请重新登录');location.href='/login'</script>")
    }
}

//格式化时间的封装函数
exports.dateFormat = function(value){
    var time = new Date(value);
    var year = time.getFullYear();
    var month = time.getMonth() +1;
    var day = time.getDate();
    var hour = time.getHours();
    var min = time.getMinutes();
    var sec = time.getSeconds();

    return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec; 
}


